package com.atguigu.controller;

import com.atguigu.service.SysUserService;
import com.atguigu.dto.system.LoginDto;
import com.atguigu.service.ValidateCodeService;
import com.atguigu.utils.AuthContextUtil;
import com.atguigu.vo.common.Result;
import com.atguigu.vo.common.ResultCodeEnum;
import com.atguigu.vo.system.LoginVo;
import com.atguigu.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

@Tag(name = "系统管理") // swagger
@ComponentScan(basePackages = {"com.atguigu"}) // swagger配置类不生效的问题
@RestController
@RequestMapping(value = "/system/index")
public class SysIndexController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateService;

    @Operation(summary = "登录方法") //  // swagger
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "生成验证码")
    @GetMapping("/validateCode")
    public Result<ValidateCodeVo> validateCode() {
        ValidateCodeVo validateCodeVo = validateService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }

//    @Operation(summary = "获取用户信息")
//    @GetMapping("/getUserInfo")
//    public Result getUserInfo(@RequestHeader(name = "token") String token){
//        SysUser sysUser = sysUserService.getUserInfo(token);
//        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
//    }

    @Operation(summary = "退出登录")
    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
